<template>
    <div class="listview listview-form">

        <v-field v-model="value" placeholder="提示文字..." type="textarea" rows="8" :limit="limit"
                 class="limit-outer"></v-field>

        <div class="v-comments__btn__frm">
            <v-button class="v-comments__submit" @click="submit">发布评论</v-button>
        </div>

    </div>
</template>

<script>
    import CONFIG from '../config';
    import logger from '../js/utils/logger';
    import vField from '../vendor/v-field.vue';
    import vButton from '../vendor/v-button.vue';
    import MicroComment from '../vendor/v-comment';

    export default {
        name: 'page-comment-create',

        components: {
            'v-field': vField,
            'v-button': vButton
        },

        data () {
            let _self = this;
            return {
                subjectId: this.$route.params.subjectId || 0,
                userId: (CONFIG.loginData.userInfo.userId > 0) ? CONFIG.loginData.userInfo.userId : 0,
                toId: _self.$route.query.toid / 1 || 0,
                toUserId: _self.$route.query.touid / 1 || 0,
                value: '',
                limit: CONFIG.COMMENT_MAX
            };
        },

        mounted () {
            logger.log('comment-create mounted... ', this.$route.params.id);
        },

        methods: {
            submit () {
                let _self = this;

                logger.log('comment-create.submit: ', this.value);
                return MicroComment.addComment({
                    subjectId: this.$route.params.id, // this.$route.query.subjectId || 0,
                    userId: this.userId,
                    content: this.value,
                    toUserId: this.toUserId,
                    toId: this.toId,
                    parent: this.$route.query.pid
                }).then(function (res) {
                    logger.log('[comment].create.SUCSESS: ', res);
                    if (res.Code === 0) {
//                        window.history.back();
//                        let currentItem = MicroComment.getCurrentItem();
//                        currentItem.comment.CommentNum += 1;
//                        MicroComment.setCurrentItem(currentItem); // 当前缓存对象评论数+1
//                        logger.log('go back...', MicroComment.getCurrentItem());
                        _self.$router.go(-1);
                    }
                }).catch(function (e) {
                    logger.error('[comment].create.ERROR: ', e);
                });
            }
        }
    };
</script>

<style rel="stylesheet/scss" lang="scss">
    @import "../scss/variables";
    @import "../scss/mixins";

    .v-comments__btn__frm {
        width: 100%;
        height: pxTorem(64px);
        /*margin: pxTorem(10px) 0;*/
        padding: pxTorem(10px) pxTorem(11px);
        display: block;
        position: fixed;
        z-index: 9;
        left: 0;
        bottom: 0;
        font-size: pxTorem(17px);
        line-height: 1;
        background: #FFF;
        border-top: #ccc 1px solid;
    }
</style>
